<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Formcommunity extends Common
{
	public function addcommunity(){
		return $this->fetch();
	}

  public function editcommunity(){
    $city = Db::name('community')->where('community_id',input('communityid'))->find();
    $this->assign('community',$city);
    return $this->fetch();
  }


	public function add(){
	   $info = '';
     if(request()->isPost()){  	
        if(input('param.communityname')==''){
          $this->error("请填写完整信息");
        }
        $result = Db::name('community')->where('community_name',input('param.communityname'))->find();
        $file = request()->file('pic');
        $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
        if($result){
           $this->error("社区名重复");
        }
        $data=[
          'community_name'=>input('communityname'),
          'community_pic'=>$info->getFilename(),
          'community_status'=>2
       	];
        $res=Db::name('community')->insert($data);
        if ($res) {
        	$this->success("添加社区成功",'community/index');
        }
        else {
        	$this->error("添加社区失败");
        }
    }
	}

  public function edit(){
    $info = '';
     if(request()->isPost()){   
        if(input('param.communityname')==''){
          $this->error("请填写完整信息");
        }
        $result = Db::name('community')->where('community_name',input('param.communityname'))->find();
        if($result&&$result['community_id']!=input('communityid')){
           $this->error("社区名重复");
        }
        $file = request()->file('pic');
        if($file){
          $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
          $data=[
            'community_name'=>input('communityname'),
            'community_pic'=>$info->getFilename(),
          ];
        }else{
          $data=[
            'community_name'=>input('communityname'),
          ];
        }
      
        $res=Db::name('community')->where('community_id',input('communityid'))->update($data);
        if ($res!==false){
          $this->success("修改社区名成功",'community/index');
        }
        else {
          $this->error("修改社区名失败");
        }
      }
  }

  public function delete(){
    $res = Db::name('community')->where('community_id',input('communityid'))->update(['community_status'=>input('statu')]);
    if($res===false){
        $this->error("处理失败");
    }
    $this->redirect('community/index');
  }
}